/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifpe.dao;

import ifpe.entities.Usuario;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Sostenes
 */
public class UsuarioDaoImpl extends Dao implements UsuarioDao {

    private PessoaDao pessoaDao;
    private GrupoDao grupoDao;

    public UsuarioDaoImpl() {
        pessoaDao = DaoFactory.getInstance().getPessoaDao();
        grupoDao = DaoFactory.getInstance().getGrupoDao();
    }

    @Override
    public void insert(Usuario usuario) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        pessoaDao.insert(usuario);
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(
                    "insert into usuario(login, senha, id_pessoa, id_grupo) values(?, ?, ?, ?)");
            pstmt.setString(1, usuario.getLogin());
            pstmt.setString(2, usuario.getSenha());
            pstmt.setLong(3, usuario.getIdPessoa());
            pstmt.setLong(4, usuario.getGrupo().getId());
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();

            if (rs.next()) {
                usuario.setId(rs.getLong(1));
            }
        } catch (SQLException ex) {
            throw new DaoException(ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
        }
    }

    @Override
    public void update(Usuario usuario) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        pessoaDao.update(usuario);
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(
                    "update usuario set login = ?, senha = ?, id_grupo = ? where id = ?");
            pstmt.setString(1, usuario.getLogin());
            pstmt.setString(2, usuario.getSenha());
            pstmt.setLong(3, usuario.getGrupo().getId());
            pstmt.setLong(4, usuario.getId());
            pstmt.executeUpdate();
            
        } catch (SQLException ex) {
            throw new DaoException(ex);
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
        }
    }

    @Override
    public Usuario get(long id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Usuario usuario = null;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(
                    "select id, login, senha, id_pessoa, id_grupo from usuario where id = ?");
            pstmt.setLong(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                usuario = new Usuario();
                usuario = (Usuario) pessoaDao.get(rs.getLong("id_pessoa"), usuario);
                usuario.setId(rs.getLong("id"));
                usuario.setLogin(rs.getString("login"));
                usuario.setSenha(rs.getString("senha"));
                usuario.setGrupo(grupoDao.get(rs.getLong("id_grupo")));
            }
        } catch (SQLException ex) {
            throw new DaoException(ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
        }
        return usuario;
    }

    @Override
    public Usuario get(String login, String senha) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Usuario usuario = null;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(
                    "select id, login, senha, id_pessoa, id_grupo from usuario where login = ? and senha = ?");
            pstmt.setString(1, login);
            pstmt.setString(2, senha);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                usuario = new Usuario();
                usuario = (Usuario) pessoaDao.get(rs.getLong("id_pessoa"), usuario);
                usuario.setId(rs.getLong("id"));
                usuario.setLogin(rs.getString("login"));
                usuario.setSenha(rs.getString("senha"));
                usuario.setGrupo(grupoDao.get(rs.getLong("id_grupo")));
            }
        } catch (SQLException ex) {
            throw new DaoException(ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
        }
        return usuario;
    }

    @Override
    public void delete(Usuario usuario) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean exists(Usuario usuario) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean exists = false;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(
                    "select login from usuario where login = ?");
            pstmt.setString(1, usuario.getLogin());
            rs = pstmt.executeQuery();

            if (rs.next()) {
                exists = true;
            }
        } catch (SQLException ex) {
            throw new DaoException(ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException ex) {
                throw new DaoException(ex);
            }
        }
        return exists;
    }
}
